<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="#课程目标  &#x3D;&#x3D;掌握for循环语句的基本语法结构&#x3D;&#x3D; &#x3D;&#x3D;掌握while和until循环语句的基本语法结构&#x3D;&#x3D; 能会使用RANDOM产生随机数 理解嵌套循环">
<meta property="og:type" content="article">
<meta property="og:title" content="05_SHELL编程之嵌套循环+随机数及综合案例">
<meta property="og:url" content="http://example.com/2021/01/05/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/05_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF+%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%8F%8A%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="#课程目标  &#x3D;&#x3D;掌握for循环语句的基本语法结构&#x3D;&#x3D; &#x3D;&#x3D;掌握while和until循环语句的基本语法结构&#x3D;&#x3D; 能会使用RANDOM产生随机数 理解嵌套循环">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-05T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-17T07:38:07.119Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/01/05/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/05_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF+%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%8F%8A%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>05_SHELL编程之嵌套循环+随机数及综合案例 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/05/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/05_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF+%E9%9A%8F%E6%9C%BA%E6%95%B0%E5%8F%8A%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          05_SHELL编程之嵌套循环+随机数及综合案例
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-05 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-05T20:46:25+08:00">2021-01-05</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-17 15:38:07" itemprop="dateModified" datetime="2021-02-17T15:38:07+08:00">2021-02-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>14k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>13 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>#课程目标</p>
<ul>
<li>==掌握for循环语句的基本语法结构==</li>
<li>==掌握while和until循环语句的基本语法结构==</li>
<li>能会使用RANDOM产生随机数</li>
<li>理解嵌套循环</li>
</ul>
<a id="more"></a>

<h1 id="一、随机数"><a href="#一、随机数" class="headerlink" title="一、随机数"></a>一、随机数</h1><p><strong>关键词：一切都是未知数，永远不知道明天会抽什么风</strong>:wind_chime::sweat_smile:</p>
<h2 id="1-如何生成随机数？"><a href="#1-如何生成随机数？" class="headerlink" title="1. 如何生成随机数？"></a>1. 如何生成随机数？</h2><p><strong>系统变量</strong>：**==RANDOM==**，默认会产生0~32767的随机整数</p>
<p><strong>前言：</strong>要想调用变量，不管你是什么变量都要给钱，而且是美元:heavy_dollar_sign:</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">打印一个随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$RANDOM</span></span><br><span class="line">查看系统上一次生成的随机数</span><br><span class="line"><span class="comment"># set|grep RANDOM</span></span><br><span class="line">RANDOM=<span class="number">28325</span></span><br><span class="line"></span><br><span class="line">产生<span class="number">0</span>~<span class="number">1</span>之间的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">2</span>]</span><br><span class="line"></span><br><span class="line">产生<span class="number">0</span>~<span class="number">2</span>之间的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">3</span>]</span><br><span class="line"></span><br><span class="line">产生<span class="number">0</span>~<span class="number">3</span>之间的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">4</span>]</span><br><span class="line"></span><br><span class="line">产生<span class="number">0</span>~<span class="number">9</span>内的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line"></span><br><span class="line">产生<span class="number">0</span>~<span class="number">100</span>内的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">101</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">产生<span class="number">50</span><span class="literal">-100</span>之内的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">51</span>+<span class="number">50</span>]</span><br><span class="line"></span><br><span class="line">产生三位数的随机数</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">900</span>+<span class="number">100</span>]</span><br></pre></td></tr></table></figure>
<h2 id="2-实战案例"><a href="#2-实战案例" class="headerlink" title="2. 实战案例"></a>2. 实战案例</h2><h3 id="㈠-随机产生以139开头的电话号码"><a href="#㈠-随机产生以139开头的电话号码" class="headerlink" title="㈠ 随机产生以139开头的电话号码"></a>㈠ 随机产生以139开头的电话号码</h3><p><strong>具体需求1：</strong></p>
<p>写一个脚本，产生一个phonenum.txt文件，随机产生以139开头的手机号1000个，每个一行。</p>
<h4 id="①-思路"><a href="#①-思路" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>产生1000个电话号码，脚本需要循环1000次 <code>FOR WHILE UNTIL</code></li>
<li>139+8位,后8位随机产生，可以让每一位数字都随机产生  <code>echo $[$RANDOM%10]</code></li>
<li>将随机产生的数字分别保存到变量里，然后加上139保存到文件里</li>
</ol>
<h4 id="②-落地实现"><a href="#②-落地实现" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment">#产生1000个以139开头的电话号码并保存文件phonenum.txt</span></span><br><span class="line">file=/shell03/phonenum.txt</span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">1000</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> &gt;&gt; <span class="variable">$file</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># random phonenum</span></span><br><span class="line"><span class="comment"># 循环1000次产生电话号码并保存到文件</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1</span>..<span class="number">1000</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> &gt;&gt; phonenum.txt</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -<span class="type">le</span> <span class="number">1000</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> &gt;&gt; phonenum.txt</span><br><span class="line">	let i++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="keyword">continue</span>:继续，跳过本次循环，执行下一次循环</span><br><span class="line"><span class="keyword">break</span>:打断，执行循环体外的代码do..done外</span><br><span class="line"><span class="keyword">exit</span>:退出程序</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1</span>..<span class="number">1000</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> &gt;&gt; phonenum.txt</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#create phone num file</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">1000</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> |<span class="built_in">tee</span> <span class="literal">-a</span> phonenum.txt</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">count=<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> true</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	n1=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n2=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n3=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n4=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n5=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n6=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n7=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	n8=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">10</span>]</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139<span class="variable">$n1</span><span class="variable">$n2</span><span class="variable">$n3</span><span class="variable">$n4</span><span class="variable">$n5</span><span class="variable">$n6</span><span class="variable">$n7</span><span class="variable">$n8</span>&quot;</span> |<span class="built_in">tee</span> <span class="literal">-a</span> phonenum.txt &amp;&amp; let count++</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$count</span> -<span class="type">eq</span> <span class="number">1000</span> ];then</span><br><span class="line">		<span class="keyword">break</span></span><br><span class="line">	fi</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="㈡-随机抽出5位幸运观众"><a href="#㈡-随机抽出5位幸运观众" class="headerlink" title="㈡ 随机抽出5位幸运观众"></a>㈡ 随机抽出5位幸运观众</h3><p><strong>具体需求：</strong></p>
<ol>
<li>在上面的1000个手机号里抽奖==5个==幸运观众，显示出这5个幸运观众。</li>
<li>但只显示头3个数和尾号的4个数，中间的都用*代替</li>
</ol>
<h4 id="①-思路-1"><a href="#①-思路-1" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>确定幸运观众所在的行    <code>0-1000  随机找出一个数字   $[$RANDOM%1000+1]</code></li>
<li>将电话号码提取出来      <code>head -随机产生行号 phonenum.txt |tail -1</code></li>
<li>==显示==前3个和后4个数到屏幕   <code>echo 139****</code></li>
</ol>
<h4 id="②-落地实现-1"><a href="#②-落地实现-1" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">phone=/shell03/phonenum.txt</span><br><span class="line"><span class="comment">#循环抽出5位幸运观众</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="comment">#定位幸运观众所在行号</span></span><br><span class="line">	line=`wc <span class="literal">-l</span> <span class="variable">$phone</span> |cut <span class="literal">-d</span><span class="string">&#x27; &#x27;</span> <span class="literal">-f1</span>`</span><br><span class="line">	luck_line=<span class="variable">$</span>[<span class="type">RANDOM</span>%<span class="variable">$line</span>+<span class="number">1</span>]</span><br><span class="line">	<span class="comment">#取出幸运观众所在行的电话号码</span></span><br><span class="line">	luck_num=`head -<span class="variable">$luck_line</span> <span class="variable">$phone</span>|tail <span class="literal">-1</span>`</span><br><span class="line">	<span class="comment">#显示到屏幕</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139****<span class="variable">$</span>&#123;luck_num:7:4&#125;&quot;</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$luck_num</span> &gt;&gt; luck.txt</span><br><span class="line">	<span class="comment">#删除已经被抽取的幸运观众号码</span></span><br><span class="line">	<span class="comment">#sed -i &quot;/$luck_num/d&quot; $phone</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">file=/shell04/phonenum.txt</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1</span>..<span class="number">5</span>&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	file_num=`wc <span class="literal">-l</span> <span class="variable">$file</span> |cut <span class="literal">-d</span><span class="string">&#x27; &#x27;</span> <span class="literal">-f1</span>`</span><br><span class="line">	line=`echo <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="variable">$file_num</span>+<span class="number">1</span>]`</span><br><span class="line">	luck=`head <span class="literal">-n</span> <span class="variable">$line</span>  <span class="variable">$file</span>|tail <span class="literal">-1</span>`</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;139****<span class="variable">$</span>&#123;luck:7:4&#125;&quot;</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$luck</span> &gt;&gt; /shell04/luck_num.txt</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">file=phonenum.txt</span><br><span class="line">line=`cat phonenum.txt |wc <span class="literal">-l</span>`	<span class="number">1000</span></span><br><span class="line">luckline=<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="variable">$line</span>+<span class="number">1</span>]</span><br><span class="line">phone=`cat <span class="variable">$file</span>|head -<span class="variable">$luckline</span>|tail <span class="literal">-1</span>`</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;幸运观众为:139****<span class="variable">$</span>&#123;phone:7:4&#125;&quot;</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># choujiang</span></span><br><span class="line">phone=phonenum.txt</span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	num=`wc <span class="literal">-l</span> phonenum.txt |cut <span class="literal">-d</span><span class="string">&#x27; &#x27;</span> <span class="literal">-f1</span>`</span><br><span class="line">	line=`echo <span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="variable">$num</span>+<span class="number">1</span>]`</span><br><span class="line">	luck=`head -<span class="variable">$line</span> <span class="variable">$phone</span> |tail <span class="literal">-1</span>`</span><br><span class="line">	sed <span class="literal">-i</span> <span class="string">&quot;/<span class="variable">$luck</span>/d&quot;</span> <span class="variable">$phone</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;幸运观众是:139****<span class="variable">$</span>&#123;luck:7:4&#125;&quot;</span></span><br><span class="line">done</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈢-批量创建用户-密码随机产生"><a href="#㈢-批量创建用户-密码随机产生" class="headerlink" title="㈢ 批量创建用户(密码随机产生)"></a>㈢ 批量创建用户(密码随机产生)</h3><p><strong>需求：</strong>批量创建5个用户，每个用户的密码为一个随机数</p>
<h4 id="①-思路-2"><a href="#①-思路-2" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>循环5次创建用户</li>
<li>产生一个密码文件来保存用户的随机密码</li>
<li>从密码文件中取出随机密码赋值给用户</li>
</ol>
<h4 id="②-落地实现-2"><a href="#②-落地实现-2" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#crate user and set passwd</span></span><br><span class="line"><span class="comment">#产生一个保存用户名和密码的文件</span></span><br><span class="line"><span class="built_in">echo</span> user0&#123;<span class="number">1</span>..<span class="number">5</span>&#125;:itcast<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">9000</span>+<span class="number">1000</span>]<span class="comment">#@~|tr &#x27; &#x27; &#x27;\n&#x27;&gt;&gt; user_pass.file</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#循环创建5个用户</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	user=`head -<span class="variable">$i</span> user_pass.file|tail <span class="literal">-1</span>|cut <span class="literal">-d</span>: <span class="literal">-f1</span>`</span><br><span class="line">	pass=`head -<span class="variable">$i</span> user_pass.file|tail <span class="literal">-1</span>|cut <span class="literal">-d</span>: <span class="literal">-f2</span>`</span><br><span class="line">	useradd <span class="variable">$user</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$pass</span>|passwd -<span class="literal">-stdin</span> <span class="variable">$user</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `cat user_pass.file`</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	user=`echo <span class="variable">$i</span>|cut <span class="literal">-d</span>: <span class="literal">-f1</span>`</span><br><span class="line">	pass=`echo <span class="variable">$i</span>|cut <span class="literal">-d</span>: <span class="literal">-f2</span>`</span><br><span class="line">	useradd <span class="variable">$user</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$pass</span>|passwd -<span class="literal">-stdin</span> <span class="variable">$user</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#crate user and set passwd</span></span><br><span class="line"><span class="comment">#产生一个保存用户名和密码的文件</span></span><br><span class="line"><span class="built_in">echo</span> user0&#123;<span class="number">1</span>..<span class="number">3</span>&#125;:itcast<span class="variable">$</span>[<span class="variable">$RANDOM</span>%<span class="number">9000</span>+<span class="number">1000</span>]<span class="comment">#@~|tr &#x27; &#x27; &#x27;\n&#x27;|tr &#x27;:&#x27; &#x27; &#x27; &gt;&gt; user_pass.file</span></span><br><span class="line"><span class="comment">#循环创建5个用户</span></span><br><span class="line"><span class="keyword">while</span> read user pass</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">useradd <span class="variable">$user</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$pass</span>|passwd -<span class="literal">-stdin</span> <span class="variable">$user</span></span><br><span class="line">done &lt; user_pass.file</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pwgen工具产生随机密码：</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell04</span>]<span class="comment"># pwgen -cn1 12</span></span><br><span class="line">Meep5ob1aesa</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell04</span>]<span class="comment"># echo user0&#123;1..3&#125;:$(pwgen -cn1 12)</span></span><br><span class="line">user01:Bahqu9haipho user02:Feiphoh7moo4 user03:eilahj5eth2<span class="built_in">R</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell04</span>]<span class="comment"># echo user0&#123;1..3&#125;:$(pwgen -cn1 12)|tr &#x27; &#x27; &#x27;\n&#x27;</span></span><br><span class="line">user01:eiwaShuZo5hi</span><br><span class="line">user02:eiDeih7aim9k</span><br><span class="line">user03:aeBahwien8co</span><br></pre></td></tr></table></figure>
<h1 id="二、嵌套循环"><a href="#二、嵌套循环" class="headerlink" title="二、嵌套循环"></a>二、嵌套循环</h1><p><strong>关键字：大圈套小圈</strong></p>
<p>:clock3:<strong>时钟</strong>：分针与秒针，秒针转⼀圈（60格），分针转1格。循环嵌套就是外层循环⼀次，内层循环⼀轮。</p>
<ol>
<li>一个==循环体==内又包含另一个<strong>完整</strong>的循环结构，称为循环的嵌套。</li>
<li>每次外部循环都会==触发==内部循环，直至内部循环完成，才接着执行下一次的外部循环。</li>
<li>for循环、while循环和until循环可以<strong>相互</strong>嵌套。</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> (())</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	</span><br><span class="line">	done</span><br><span class="line"></span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>##1. 应用案例</p>
<h3 id="㈠-打印指定图案"><a href="#㈠-打印指定图案" class="headerlink" title="㈠ 打印指定图案"></a>㈠ 打印指定图案</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">12</span></span><br><span class="line"><span class="number">123</span></span><br><span class="line"><span class="number">1234</span></span><br><span class="line"><span class="number">12345</span></span><br><span class="line"></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">54</span></span><br><span class="line"><span class="number">543</span></span><br><span class="line"><span class="number">5432</span></span><br><span class="line"><span class="number">54321</span></span><br><span class="line"></span><br><span class="line">外部循环：打印换行，并且换<span class="number">5</span>行 ，循环<span class="number">5</span>次</span><br><span class="line"></span><br><span class="line">内部循环：打印<span class="number">54321</span>数字</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ((y=<span class="number">5</span>;y&gt;=<span class="number">1</span>;y--))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> ((x=<span class="number">5</span>;x&gt;=<span class="number">1</span>;x--))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-落地实现1"><a href="#㈡-落地实现1" class="headerlink" title="㈡ 落地实现1"></a>㈡ 落地实现1</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">X轴：</span><br><span class="line"><span class="keyword">for</span> ((i=<span class="number">1</span>;i&lt;=<span class="number">5</span>;i++));<span class="keyword">do</span> <span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$i</span>;done</span><br><span class="line">Y轴：</span><br><span class="line">负责打印换行</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> ((y=<span class="number">1</span>;y&lt;=<span class="number">5</span>;y++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> ((x=<span class="number">1</span>;x&lt;=<span class="variable">$y</span>;x++))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> ((y=<span class="number">1</span>;y&lt;=<span class="number">5</span>;y++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	x=<span class="number">1</span></span><br><span class="line">	<span class="keyword">while</span> [ <span class="variable">$x</span> -<span class="type">le</span> <span class="variable">$y</span> ]</span><br><span class="line">		<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">		let x++</span><br><span class="line">		done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<h3 id="㈢-落地实现2"><a href="#㈢-落地实现2" class="headerlink" title="㈢ 落地实现2"></a>㈢ 落地实现2</h3><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">Y轴：打印换行</span><br><span class="line">X轴：打印数字 <span class="number">5</span><span class="literal">-1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">y=<span class="number">5</span></span><br><span class="line"><span class="keyword">while</span> (( <span class="variable">$y</span> &gt;= <span class="number">1</span> ))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> ((x=<span class="number">5</span>;x&gt;=<span class="variable">$y</span>;x--))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y--</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> (( y=<span class="number">5</span>;y&gt;=<span class="number">1</span>;y--))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> (( x=<span class="number">5</span>;x&gt;=<span class="variable">$y</span>;x--))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">y=<span class="number">5</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$y</span> -<span class="type">ge</span> <span class="number">1</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> ((x=<span class="number">5</span>;x&gt;=<span class="variable">$y</span>;x--))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y--</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">y=<span class="number">1</span></span><br><span class="line"><span class="keyword">until</span> (( <span class="variable">$y</span> &gt;<span class="number">5</span> ))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	x=<span class="number">1</span></span><br><span class="line">	<span class="keyword">while</span> (( <span class="variable">$x</span> &lt;= <span class="variable">$y</span> ))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$</span>[<span class="number">6</span>-<span class="variable">$x</span>]</span><br><span class="line">	let x++</span><br><span class="line">	done	</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">y=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> (( <span class="variable">$y</span>&lt;= <span class="number">5</span> ))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        <span class="keyword">for</span> ((x=<span class="number">5</span>;x&gt;=<span class="number">6</span>-<span class="variable">$y</span>;x--))</span><br><span class="line">        <span class="keyword">do</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="literal">-n</span> <span class="variable">$x</span></span><br><span class="line"></span><br><span class="line">        done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">课后打印：</span><br><span class="line"><span class="number">54321</span></span><br><span class="line"><span class="number">5432</span></span><br><span class="line"><span class="number">543</span></span><br><span class="line"><span class="number">54</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>##2. 课堂练习</p>
<p><strong>打印九九乘法表（三种方法）</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>*<span class="number">1</span>=<span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">2</span>=<span class="number">2</span>   <span class="number">2</span>*<span class="number">2</span>=<span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">3</span>=<span class="number">3</span>   <span class="number">2</span>*<span class="number">3</span>=<span class="number">6</span>   <span class="number">3</span>*<span class="number">3</span>=<span class="number">9</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">4</span>=<span class="number">4</span>   <span class="number">2</span>*<span class="number">4</span>=<span class="number">8</span>   <span class="number">3</span>*<span class="number">4</span>=<span class="number">12</span>  <span class="number">4</span>*<span class="number">4</span>=<span class="number">16</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">5</span>=<span class="number">5</span>   <span class="number">2</span>*<span class="number">5</span>=<span class="number">10</span>  <span class="number">3</span>*<span class="number">5</span>=<span class="number">15</span>  <span class="number">4</span>*<span class="number">5</span>=<span class="number">20</span>  <span class="number">5</span>*<span class="number">5</span>=<span class="number">25</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">6</span>=<span class="number">6</span>   <span class="number">2</span>*<span class="number">6</span>=<span class="number">12</span>  <span class="number">3</span>*<span class="number">6</span>=<span class="number">18</span>  <span class="number">4</span>*<span class="number">6</span>=<span class="number">24</span>  <span class="number">5</span>*<span class="number">6</span>=<span class="number">30</span>  <span class="number">6</span>*<span class="number">6</span>=<span class="number">36</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">7</span>=<span class="number">7</span>   <span class="number">2</span>*<span class="number">7</span>=<span class="number">14</span>  <span class="number">3</span>*<span class="number">7</span>=<span class="number">21</span>  <span class="number">4</span>*<span class="number">7</span>=<span class="number">28</span>  <span class="number">5</span>*<span class="number">7</span>=<span class="number">35</span>  <span class="number">6</span>*<span class="number">7</span>=<span class="number">42</span>  <span class="number">7</span>*<span class="number">7</span>=<span class="number">49</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">8</span>=<span class="number">8</span>   <span class="number">2</span>*<span class="number">8</span>=<span class="number">16</span>  <span class="number">3</span>*<span class="number">8</span>=<span class="number">24</span>  <span class="number">4</span>*<span class="number">8</span>=<span class="number">32</span>  <span class="number">5</span>*<span class="number">8</span>=<span class="number">40</span>  <span class="number">6</span>*<span class="number">8</span>=<span class="number">48</span>  <span class="number">7</span>*<span class="number">8</span>=<span class="number">56</span>  <span class="number">8</span>*<span class="number">8</span>=<span class="number">64</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>*<span class="number">9</span>=<span class="number">9</span>   <span class="number">2</span>*<span class="number">9</span>=<span class="number">18</span>  <span class="number">3</span>*<span class="number">9</span>=<span class="number">27</span>  <span class="number">4</span>*<span class="number">9</span>=<span class="number">36</span>  <span class="number">5</span>*<span class="number">9</span>=<span class="number">45</span>  <span class="number">6</span>*<span class="number">9</span>=<span class="number">54</span>  <span class="number">7</span>*<span class="number">9</span>=<span class="number">63</span>  <span class="number">8</span>*<span class="number">9</span>=<span class="number">72</span>  <span class="number">9</span>*<span class="number">9</span>=<span class="number">81</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Y轴：循环<span class="number">9</span>次，打印<span class="number">9</span>行空行</span><br><span class="line">X轴：循环次数和Y轴相关；打印的是X和Y轴乘积 <span class="variable">$</span>[] <span class="variable">$</span>(())</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> ((y=<span class="number">1</span>;y&lt;=<span class="number">9</span>;y++))</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	<span class="keyword">for</span> ((x=<span class="number">1</span>;x&lt;=<span class="variable">$y</span>;x++))</span><br><span class="line">	<span class="keyword">do</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="operator">-ne</span> <span class="string">&quot;<span class="variable">$x</span>*<span class="variable">$y</span>=<span class="variable">$</span>[<span class="variable">$x</span>*<span class="variable">$y</span>]\t&quot;</span></span><br><span class="line">	done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">y=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$y</span> -<span class="type">le</span> <span class="number">9</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        x=<span class="number">1</span></span><br><span class="line">        <span class="keyword">while</span> [ <span class="variable">$x</span> -<span class="type">le</span> <span class="variable">$y</span> ]</span><br><span class="line">        <span class="keyword">do</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="operator">-ne</span> <span class="string">&quot;<span class="variable">$x</span>*<span class="variable">$y</span>=<span class="variable">$</span>[<span class="variable">$x</span>*<span class="variable">$y</span>]\t&quot;</span></span><br><span class="line">                let x++</span><br><span class="line">        done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq <span class="number">9</span>`</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> `seq <span class="variable">$i</span>`</span><br><span class="line">    <span class="keyword">do</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="operator">-ne</span>  <span class="string">&quot;<span class="variable">$j</span>*<span class="variable">$i</span>=<span class="variable">$</span>[<span class="variable">$i</span>*<span class="variable">$j</span>]\t&quot;</span></span><br><span class="line">    done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">done</span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">y=<span class="number">1</span></span><br><span class="line"><span class="keyword">until</span> [ <span class="variable">$y</span> -<span class="type">gt</span> <span class="number">9</span> ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        x=<span class="number">1</span></span><br><span class="line">        <span class="keyword">until</span> [ <span class="variable">$x</span> -<span class="type">gt</span> <span class="variable">$y</span> ]</span><br><span class="line">        <span class="keyword">do</span></span><br><span class="line">                <span class="built_in">echo</span> <span class="operator">-ne</span> <span class="string">&quot;<span class="variable">$x</span>*<span class="variable">$y</span>=<span class="variable">$</span>[ <span class="variable">$x</span>*<span class="variable">$y</span> ]\t&quot;</span></span><br><span class="line">                let x++</span><br><span class="line">        done</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line">let y++</span><br><span class="line">done</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="三、阶段性补充总结"><a href="#三、阶段性补充总结" class="headerlink" title="三、阶段性补充总结"></a>三、阶段性补充总结</h1><h2 id="1-变量定义"><a href="#1-变量定义" class="headerlink" title="1. 变量定义"></a>1. 变量定义</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1）变量名&#x3D;变量值</span><br><span class="line">echo $变量名</span><br><span class="line">echo $&#123;变量名&#125;</span><br><span class="line"></span><br><span class="line">2）read -p &quot;提示用户信息:&quot; 变量名</span><br><span class="line"></span><br><span class="line">3） declare -i&#x2F;-x&#x2F;-r  变量名&#x3D;变量值</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="2-流程控制语句"><a href="#2-流程控制语句" class="headerlink" title="2. 流程控制语句"></a>2. 流程控制语句</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>）<span class="keyword">if</span> [ 条件判断 ];then</span><br><span class="line">		command</span><br><span class="line">	fi</span><br><span class="line">	</span><br><span class="line"><span class="number">2</span>) <span class="keyword">if</span> [ 条件判断 ];then</span><br><span class="line">		command</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">   	command</span><br><span class="line">   fi</span><br><span class="line">   </span><br><span class="line"> <span class="number">3</span>) <span class="keyword">if</span> [ 条件判断<span class="number">1</span> ];then</span><br><span class="line"> 		command1</span><br><span class="line"> 	 elif [ 条件判断<span class="number">2</span> ];then</span><br><span class="line"> 	 	command2</span><br><span class="line"> 	 <span class="keyword">else</span></span><br><span class="line">    	command3</span><br><span class="line">   fi 	</span><br><span class="line">   </span><br></pre></td></tr></table></figure>
<h2 id="3-循环语句"><a href="#3-循环语句" class="headerlink" title="3. 循环语句"></a>3. 循环语句</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">目的：某个动作重复去做，用到循环</span><br><span class="line"><span class="keyword">for</span></span><br><span class="line"><span class="keyword">while</span></span><br><span class="line"><span class="keyword">until</span></span><br></pre></td></tr></table></figure>
<h2 id="4-影响shell程序的内置命令"><a href="#4-影响shell程序的内置命令" class="headerlink" title="4. 影响shell程序的内置命令"></a>4. 影响shell程序的内置命令</h2><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">exit</span>			退出整个程序</span><br><span class="line"><span class="keyword">break</span>		   结束当前循环，或跳出本层循环</span><br><span class="line"><span class="keyword">continue</span> 	忽略本次循环剩余的代码，直接进行下一次循环</span><br><span class="line">shift			使位置参数向左移动，默认移动<span class="number">1</span>位，可以使用shift <span class="number">2</span></span><br><span class="line"></span><br><span class="line">:</span><br><span class="line">true</span><br><span class="line">false</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>举例说明：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">以下脚本都能够实现用户自定义输入数字，然后脚本计算和：</span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># cat shift.sh </span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">sum=<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$</span><span class="comment"># -ne 0 ]</span></span><br><span class="line"><span class="type">do</span></span><br><span class="line"><span class="type">let</span> <span class="type">sum</span>=<span class="variable">$sum</span>+<span class="variable">$1</span></span><br><span class="line"><span class="type">shift</span></span><br><span class="line"><span class="type">done</span></span><br><span class="line"><span class="type">echo</span> <span class="type">sum</span>=<span class="variable">$sum</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">MissHou</span> <span class="type">shell04</span>]<span class="comment"># cat for3.sh </span></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="type">sum</span>=<span class="number">0</span></span><br><span class="line"><span class="type">for</span> <span class="type">i</span></span><br><span class="line"><span class="type">do</span></span><br><span class="line"><span class="type">let</span> <span class="type">sum</span>=<span class="variable">$sum</span>+<span class="variable">$i</span></span><br><span class="line"><span class="type">done</span></span><br><span class="line"><span class="type">echo</span> <span class="type">sum</span>=<span class="variable">$sum</span></span><br></pre></td></tr></table></figure>
<p>##5. 补充扩展expect</p>
<p>expect 自动应答  tcl语言</p>
<p><strong>需求1：</strong>A远程登录到server上什么都不做</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/expect</span></span><br><span class="line"><span class="comment"># 开启一个程序</span></span><br><span class="line">spawn ssh root@<span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="comment"># 捕获相关内容</span></span><br><span class="line">expect &#123;</span><br><span class="line">        <span class="string">&quot;(yes/no)?&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">        <span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;123456\r&quot;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line">interact   //交互</span><br><span class="line"></span><br><span class="line">脚本执行方式：</span><br><span class="line"><span class="comment"># ./expect1.sh</span></span><br><span class="line"><span class="comment"># /shell04/expect1.sh</span></span><br><span class="line"><span class="comment"># expect -f expect1.sh</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>）定义变量</span><br><span class="line"><span class="comment">#!/usr/bin/expect</span></span><br><span class="line"><span class="built_in">set</span> ip <span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="built_in">set</span> pass <span class="number">123456</span></span><br><span class="line"><span class="built_in">set</span> timeout <span class="number">5</span></span><br><span class="line">spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">expect &#123;</span><br><span class="line">	<span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">	<span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line">interact</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">2</span>）使用位置参数</span><br><span class="line"><span class="comment">#!/usr/bin/expect</span></span><br><span class="line"><span class="built_in">set</span> ip [ <span class="type">lindex</span> <span class="variable">$argv</span> <span class="number">0</span> ]</span><br><span class="line"><span class="built_in">set</span> pass [ <span class="type">lindex</span> <span class="variable">$argv</span> <span class="number">1</span> ]</span><br><span class="line"><span class="built_in">set</span> timeout <span class="number">5</span></span><br><span class="line">spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">expect &#123;</span><br><span class="line">	<span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">	<span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line">interact</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>需求2：</strong>A远程登录到server上操作</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/expect</span></span><br><span class="line"><span class="built_in">set</span> ip <span class="number">10.1</span>.<span class="number">1.1</span></span><br><span class="line"><span class="built_in">set</span> pass <span class="number">123456</span></span><br><span class="line"><span class="built_in">set</span> timeout <span class="number">5</span></span><br><span class="line">spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">expect &#123;</span><br><span class="line">	<span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">	<span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">expect <span class="string">&quot;#&quot;</span></span><br><span class="line">send <span class="string">&quot;rm -rf /tmp/*\r&quot;</span></span><br><span class="line">send <span class="string">&quot;touch /tmp/file&#123;1..3&#125;\r&quot;</span></span><br><span class="line">send <span class="string">&quot;date\r&quot;</span></span><br><span class="line">send <span class="string">&quot;exit\r&quot;</span></span><br><span class="line">expect eof</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>需求3：</strong>shell脚本和expect结合使用，在==多台服务器==上创建1个用户</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">server</span> <span class="type">shell04</span>]<span class="comment"># cat ip.txt </span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.1</span> <span class="number">123456</span></span><br><span class="line"><span class="number">10.1</span>.<span class="number">1.2</span> <span class="number">123456</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>. 循环  useradd username</span><br><span class="line"><span class="number">2</span>. 登录远程主机——&gt;ssh——&gt;从ip.txt文件里获取IP和密码分别赋值给两个变量</span><br><span class="line"><span class="number">3</span>. 使用expect程序来解决交互问题</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># 循环在指定的服务器上创建用户和文件</span></span><br><span class="line"><span class="keyword">while</span> read ip pass</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	/usr/bin/expect &lt;&lt;<span class="literal">-END</span> &amp;&gt;/dev/null</span><br><span class="line">	spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">	expect &#123;</span><br><span class="line">	<span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">	<span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">	&#125;</span><br><span class="line">	expect <span class="string">&quot;#&quot;</span> &#123; send <span class="string">&quot;useradd yy1;rm -rf /tmp/*;exit\r&quot;</span> &#125;</span><br><span class="line">	expect eof</span><br><span class="line">	<span class="keyword">END</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$ip</span>服务器用户创建完毕&quot;</span></span><br><span class="line">done &lt; ip.txt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="built_in">cat</span> ip.txt|<span class="keyword">while</span> read ip pass</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        &#123;</span><br><span class="line"></span><br><span class="line">        /usr/bin/expect &lt;&lt;<span class="literal">-HOU</span></span><br><span class="line">        spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">        expect &#123;</span><br><span class="line">                <span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">                <span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        expect <span class="string">&quot;#&quot;</span></span><br><span class="line">        send <span class="string">&quot;hostname\r&quot;</span></span><br><span class="line">        send <span class="string">&quot;exit\r&quot;</span></span><br><span class="line">        expect eof</span><br><span class="line">        HOU</span><br><span class="line"></span><br><span class="line">        &#125;&amp;</span><br><span class="line">done</span><br><span class="line">wait</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;user is ok....&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="keyword">while</span> read ip pass</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        &#123;</span><br><span class="line"></span><br><span class="line">        /usr/bin/expect &lt;&lt;<span class="literal">-HOU</span></span><br><span class="line">        spawn ssh root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">        expect &#123;</span><br><span class="line">                <span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">                <span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        expect <span class="string">&quot;#&quot;</span></span><br><span class="line">        send <span class="string">&quot;hostname\r&quot;</span></span><br><span class="line">        send <span class="string">&quot;exit\r&quot;</span></span><br><span class="line">        expect eof</span><br><span class="line">        HOU</span><br><span class="line"></span><br><span class="line">        &#125;&amp;</span><br><span class="line">done&lt;ip.txt</span><br><span class="line">wait</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;user is ok....&quot;</span></span><br></pre></td></tr></table></figure>


<p>#四、综合案例</p>
<p>##1. 实战案例1</p>
<h3 id="㈠-具体需求"><a href="#㈠-具体需求" class="headerlink" title="㈠ 具体需求"></a>㈠ 具体需求</h3><p>写一个脚本，将跳板机上yunwei用户的公钥推送到==局域网内==可以ping通的所有机器上</p>
<p>说明：主机和密码文件已经提供</p>
<p>10.1.1.1:123456</p>
<p>10.1.1.2:123456</p>
<h3 id="㈡-案例分析"><a href="#㈡-案例分析" class="headerlink" title="㈡ 案例分析"></a>㈡ 案例分析</h3><ol>
<li>跳板机上的yunwei用户生成秘钥对<ul>
<li>判断账号是否存在  (id yunwei)</li>
<li>判断该用户是否有密钥对文件  [ -f xxx ]</li>
</ul>
</li>
<li>判断expect程序是否安装</li>
<li>判断局域网内主机是否ping通（循环判断|for while until）<ul>
<li>循环判断  for  while</li>
<li>循环体do……done    ping 主机  如果ping通 调用expect程序自动应答推送公钥</li>
</ul>
</li>
<li>测试验证是否免密登录成功</li>
</ol>
<ul>
<li>检查服务器上ssh服务端口号</li>
<li>把公钥推送成功的主机的信息保存到文件</li>
<li>关闭防火墙和selinux</li>
<li>日志记录</li>
<li>推送公钥需要自动应答expect</li>
</ul>
<h3 id="㈢-落地实现"><a href="#㈢-落地实现" class="headerlink" title="㈢ 落地实现"></a>㈢ 落地实现</h3><h4 id="①-代码拆分"><a href="#①-代码拆分" class="headerlink" title="① 代码拆分"></a>① 代码拆分</h4><p>功能1：管理员root创建yunwei用户和安装expect软件包</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 实现批量推送公钥</span></span><br><span class="line"><span class="comment"># 判断jumper上的yunwei账号是否存在</span></span><br><span class="line">&#123;</span><br><span class="line">id yunwei</span><br><span class="line">[ <span class="variable">$</span>? -<span class="type">ne</span> <span class="number">0</span> ] &amp;&amp; useradd yunwei &amp;&amp; <span class="built_in">echo</span> <span class="number">123</span>|passwd -<span class="literal">-stdin</span> yunwei</span><br><span class="line">&#125; &amp;&gt;/dev/null</span><br><span class="line"><span class="comment">#判断expect程序是否安装</span></span><br><span class="line">rpm <span class="literal">-q</span> expect</span><br><span class="line">[ <span class="variable">$</span>? -<span class="type">ne</span> <span class="number">0</span> ] &amp;&amp; yum <span class="literal">-y</span> install expect &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;expect软件已经成功安装&quot;</span></span><br></pre></td></tr></table></figure>
<p>功能2：判断主机是否ping通并且==yunwei用户==推送公钥</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 判断yunwei用户密钥对是否存在</span></span><br><span class="line">home_dir=/home/yunwei</span><br><span class="line">[ ! -<span class="type">f</span> <span class="variable">$home_dir</span>/<span class="type">.ssh</span>/<span class="type">id_rsa.pub</span> ] &amp;&amp; ssh<span class="literal">-keygen</span> <span class="literal">-P</span> <span class="string">&#x27;&#x27;</span> <span class="operator">-f</span> id_rsa &amp;&gt;/dev/null</span><br><span class="line"></span><br><span class="line"><span class="comment">#循环检查主机的网络并且进行公钥推送</span></span><br><span class="line">ip_txt=<span class="variable">$home_dir</span>/ip.txt</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `cat <span class="variable">$ip_txt</span>`</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">	ip=`echo <span class="variable">$i</span>|cut <span class="literal">-d</span>: <span class="literal">-f1</span>`</span><br><span class="line">	pass=`echo <span class="variable">$i</span>|cut <span class="literal">-d</span>: <span class="literal">-f2</span>`</span><br><span class="line">	ping <span class="literal">-c1</span> <span class="variable">$ip</span> &amp;&gt;/dev/null</span><br><span class="line">	<span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">		<span class="built_in">echo</span> <span class="variable">$ip</span> &gt;&gt; ~/ip_up.txt</span><br><span class="line">		/usr/bin/expect &lt;&lt;<span class="literal">-END</span> &amp;&gt;/dev/null</span><br><span class="line">		spawn ssh<span class="literal">-copy</span><span class="literal">-id</span> root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">		expect</span><br><span class="line">			&#123;</span><br><span class="line">            <span class="string">&quot;(yes/no)&quot;</span>  &#123; send <span class="string">&quot;yes\n&quot;</span>;exp_continue &#125;</span><br><span class="line">            <span class="string">&quot;password:&quot;</span>  &#123; send <span class="string">&quot;<span class="variable">$pass</span>\n&quot;</span> &#125;</span><br><span class="line">			&#125;</span><br><span class="line">		expect eof</span><br><span class="line">		<span class="keyword">END</span>	</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="built_in">echo</span> <span class="variable">$ip</span> &gt;&gt; <span class="variable">$home_dir</span>/ip_down.txt</span><br><span class="line">	fi</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment"># 测试验证</span></span><br><span class="line">remote_ip=`head <span class="literal">-1</span> ~/ip_up.txt`</span><br><span class="line">ssh root<span class="selector-tag">@</span><span class="variable">$remote_ip</span> hostname</span><br><span class="line">[ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;公钥推送成功&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="②-最终实现"><a href="#②-最终实现" class="headerlink" title="② 最终实现"></a>② 最终实现</h4><ol>
<li><strong>环境准备</strong></li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">jumper<span class="literal">-server</span>	有yunwei用户</span><br><span class="line"></span><br><span class="line">yunwei用户sudo授权：</span><br><span class="line">visudo</span><br><span class="line"><span class="comment">## Allow root to run any commands anywhere</span></span><br><span class="line">root    ALL=(ALL)       ALL</span><br><span class="line">yunwei  ALL=(root)      NOPASSWD:ALL,!/sbin/shutdown,!/sbin/init,!/bin/<span class="built_in">rm</span> <span class="literal">-rf</span> /</span><br><span class="line"></span><br><span class="line">解释说明：</span><br><span class="line"><span class="number">1</span>）第一个字段yunwei指定的是用户：可以是用户名，也可以是别名。每个用户设置一行，多个用户设置多行，也可以将多个用户设置成一个别名后再进行设置。</span><br><span class="line"><span class="number">2</span>）第二个字段ALL指定的是用户所在的主机：可以是ip,也可以是主机名，表示该sudo设置只在该主机上生效，ALL表示在所有主机上都生效！限制的一般都是本机，也就是限制使用这个文件的主机;一般都指定为<span class="string">&quot;ALL&quot;</span>表示所有的主机，不管文件拷到那里都可以用。比如：<span class="number">10.1</span>.<span class="number">1.1</span>=...则表示只在当前主机生效。</span><br><span class="line"><span class="number">3</span>）第三个字段（root）括号里指定的也是用户：指定以什么用户身份执行sudo，即使用sudo后可以享有所有root账号下的权限。如果要排除个别用户，可以在括号内设置，比如ALL=(ALL,!oracle,!pos)。</span><br><span class="line"><span class="number">4</span>）第四个字段ALL指定的是执行的命令：即使用sudo后可以执行所有的命令。除了关机和删除根内容以外；也可以设置别名。NOPASSWD: ALL表示使用sudo的不需要输入密码。</span><br><span class="line"><span class="number">5</span>）也可以授权给一个用户组</span><br><span class="line">	%admin ALL=(ALL) ALL	表示admin组里的所有成员可以在任何主机上以任何用户身份执行任何命令</span><br></pre></td></tr></table></figure>
<ol start="2">
<li><strong>脚本实现</strong></li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#判断公钥是否存在</span></span><br><span class="line">[ ! -<span class="type">f</span> /<span class="type">home</span>/<span class="type">yunwei</span>/<span class="type">.ssh</span>/<span class="type">id_rsa</span> ] &amp;&amp; ssh<span class="literal">-keygen</span> <span class="literal">-P</span> <span class="string">&#x27;&#x27;</span> <span class="operator">-f</span> ~/.ssh/id_rsa</span><br><span class="line"></span><br><span class="line"><span class="comment">#循环判断主机是否ping通，如果ping通推送公钥</span></span><br><span class="line">tr <span class="string">&#x27;:&#x27;</span> <span class="string">&#x27; &#x27;</span> &lt; /shell04/ip.txt|<span class="keyword">while</span> read ip pass</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">&#123;</span><br><span class="line">        ping <span class="literal">-c1</span> <span class="variable">$ip</span> &amp;&gt;/dev/null</span><br><span class="line">        <span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$ip</span> &gt;&gt; ~/ip_up.txt</span><br><span class="line">        /usr/bin/expect &lt;&lt;<span class="literal">-END</span> &amp;&gt;/dev/null</span><br><span class="line">         spawn ssh<span class="literal">-copy</span><span class="literal">-id</span> root<span class="selector-tag">@</span><span class="variable">$ip</span></span><br><span class="line">         expect &#123;</span><br><span class="line">                <span class="string">&quot;yes/no&quot;</span> &#123; send <span class="string">&quot;yes\r&quot;</span>;exp_continue &#125;</span><br><span class="line">                <span class="string">&quot;password:&quot;</span> &#123; send <span class="string">&quot;<span class="variable">$pass</span>\r&quot;</span> &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        expect eof</span><br><span class="line">        <span class="keyword">END</span></span><br><span class="line">        fi</span><br><span class="line">&#125;&amp;</span><br><span class="line">done</span><br><span class="line">wait</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;公钥已经推送完毕，正在测试....&quot;</span></span><br><span class="line"><span class="comment">#测试验证</span></span><br><span class="line">remote_ip=`tail <span class="literal">-1</span> ~/ip_up.txt`</span><br><span class="line">ssh root<span class="selector-tag">@</span><span class="variable">$remote_ip</span> hostname &amp;&gt;/dev/null</span><br><span class="line">test <span class="variable">$</span>? <span class="operator">-eq</span> <span class="number">0</span> &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;公钥成功推送完毕&quot;</span></span><br></pre></td></tr></table></figure>
<p>##2. 实战案例2</p>
<p>写一个脚本，统计web服务的不同==连接状态==个数</p>
<ol>
<li>找出查看网站连接状态的命令  <code>ss -natp|grep :80</code></li>
<li>如何统计==不同的==状态   循环去统计，需要计算</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment">#count_http_80_state</span></span><br><span class="line"><span class="comment">#统计每个状态的个数</span></span><br><span class="line"></span><br><span class="line">declare <span class="literal">-A</span> array1</span><br><span class="line">states=`ss <span class="literal">-ant</span>|grep <span class="number">80</span>|cut <span class="literal">-d</span><span class="string">&#x27; &#x27;</span> <span class="literal">-f1</span>`</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="variable">$states</span></span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        let array1[<span class="variable">$i</span>]++</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment">#通过遍历数组里的索引和元素打印出来</span></span><br><span class="line"><span class="keyword">for</span> j <span class="keyword">in</span> <span class="variable">$</span>&#123;!array1[<span class="selector-tag">@</span>]&#125;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$j:</span><span class="variable">$</span>&#123;array1[<span class="variable">$j</span>]&#125;</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>#五、课后实战</p>
<p>1、将/etc/passwd里的用户名分类，分为管理员用户，系统用户，普通用户。<br>2、写一个倒计时脚本，要求显示离2019年1月1日（元旦）的凌晨0点，还有多少天，多少时，多少分，多少秒。<br>3、写一个脚本把一个目录内的所有==空文件==都删除，最后输出删除的文件的个数。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/04_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/" rel="prev" title="04_SHELL编程之循环语句">
      <i class="fa fa-chevron-left"></i> 04_SHELL编程之循环语句
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/06/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/06_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8BCASE%E8%AF%AD%E5%8F%A5+%E5%87%BD%E6%95%B0+%E6%AD%A3%E5%88%99/" rel="next" title="06_SHELL编程之CASE语句+函数+正则">
      06_SHELL编程之CASE语句+函数+正则 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="nav-number">1.</span> <span class="nav-text">一、随机数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%A6%82%E4%BD%95%E7%94%9F%E6%88%90%E9%9A%8F%E6%9C%BA%E6%95%B0%EF%BC%9F"><span class="nav-number">1.1.</span> <span class="nav-text">1. 如何生成随机数？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B"><span class="nav-number">1.2.</span> <span class="nav-text">2. 实战案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E9%9A%8F%E6%9C%BA%E4%BA%A7%E7%94%9F%E4%BB%A5139%E5%BC%80%E5%A4%B4%E7%9A%84%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81"><span class="nav-number">1.2.1.</span> <span class="nav-text">㈠ 随机产生以139开头的电话号码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF"><span class="nav-number">1.2.1.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.2.1.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E9%9A%8F%E6%9C%BA%E6%8A%BD%E5%87%BA5%E4%BD%8D%E5%B9%B8%E8%BF%90%E8%A7%82%E4%BC%97"><span class="nav-number">1.2.2.</span> <span class="nav-text">㈡ 随机抽出5位幸运观众</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-1"><span class="nav-number">1.2.2.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0-1"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E6%89%B9%E9%87%8F%E5%88%9B%E5%BB%BA%E7%94%A8%E6%88%B7-%E5%AF%86%E7%A0%81%E9%9A%8F%E6%9C%BA%E4%BA%A7%E7%94%9F"><span class="nav-number">1.2.3.</span> <span class="nav-text">㈢ 批量创建用户(密码随机产生)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-2"><span class="nav-number">1.2.3.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0-2"><span class="nav-number">1.2.3.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%B5%8C%E5%A5%97%E5%BE%AA%E7%8E%AF"><span class="nav-number">2.</span> <span class="nav-text">二、嵌套循环</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E6%89%93%E5%8D%B0%E6%8C%87%E5%AE%9A%E5%9B%BE%E6%A1%88"><span class="nav-number">2.0.1.</span> <span class="nav-text">㈠ 打印指定图案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B01"><span class="nav-number">2.0.2.</span> <span class="nav-text">㈡ 落地实现1</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B02"><span class="nav-number">2.0.3.</span> <span class="nav-text">㈢ 落地实现2</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%98%B6%E6%AE%B5%E6%80%A7%E8%A1%A5%E5%85%85%E6%80%BB%E7%BB%93"><span class="nav-number">3.</span> <span class="nav-text">三、阶段性补充总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89"><span class="nav-number">3.1.</span> <span class="nav-text">1. 变量定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">3.2.</span> <span class="nav-text">2. 流程控制语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">3.3.</span> <span class="nav-text">3. 循环语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%BD%B1%E5%93%8Dshell%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%86%85%E7%BD%AE%E5%91%BD%E4%BB%A4"><span class="nav-number">3.4.</span> <span class="nav-text">4. 影响shell程序的内置命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%85%B7%E4%BD%93%E9%9C%80%E6%B1%82"><span class="nav-number">3.4.1.</span> <span class="nav-text">㈠ 具体需求</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E6%A1%88%E4%BE%8B%E5%88%86%E6%9E%90"><span class="nav-number">3.4.2.</span> <span class="nav-text">㈡ 案例分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.4.3.</span> <span class="nav-text">㈢ 落地实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E4%BB%A3%E7%A0%81%E6%8B%86%E5%88%86"><span class="nav-number">3.4.3.1.</span> <span class="nav-text">① 代码拆分</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E6%9C%80%E7%BB%88%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.4.3.2.</span> <span class="nav-text">② 最终实现</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
